class Solution {
    struct cmp
    {
        bool operator()(ListNode* l1, ListNode* l2)
        {
            return l1->val > l2->val;
        }
    };
  public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        priority_queue<ListNode*, vector<ListNode*>, cmp> heap; // ⼩根堆
        for (auto head : lists)
        {
            if (head != nullptr)
                heap.push(head);
        }
        ListNode* ret = new ListNode(0);
        ListNode* prev = ret;
        while (heap.size())
        {
            ListNode* t = heap.top();
            heap.pop();
            prev = prev->next = t;
            if (t->next != nullptr)
            {
                heap.push(t->next);
            }
        }
 
        ListNode* tmp = ret->next;
        free(ret);
        return tmp;
    }
};